
package com.yunfei.api.buy;

import com.yunfei.api.buy.param.CheXiaoScheduleParam;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.bo.simuBuy.DataKeyValueBo;
import com.yunfei.bo.simuBuy.ProCheXiaoStatusBo;
import com.yunfei.bo.simuBuy.ShiMingRenZhengInfoBo;
import com.yunfei.bo.simuBuy.SiMuByBo;
import com.yunfei.contants.CheXiaoScheduleContants;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.*;
import com.yunfei.service.RedisService;
import com.yunfei.service.ValidTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 私募购买流程
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = "私募购买流程",tags = "私募购买流程")
@RestController
@RequestMapping("/api/simu/buy")
public class SiMuGouMaiLiuChengApiController {
    @Autowired
    private VideoMapper videoMapperDao;//产品路演视频
    @Autowired
    private ProductPageMapper productPageDao;//产品详情
    @Autowired
    private ProductBeiAnMapper productBeiAnDao;//产品备案信息
    @Autowired
    private ProductTagMapper productTagDao;//产品标签
    @Autowired
    private ProductPingLunMapper productPingLunMapper; //产品评论
    @Autowired
    private ProductDetailMapper productDetailDao; //产品详情
    @Autowired
    private ProdAcceDtlMapper prodAcceDtlMapper; //产品附件
    @Autowired
    private ProdAnswAcceDtlMapper prodAnswAcceDtlMapper; //产品评论附件
    @Autowired
    private ValidTokenService validToken; //验证token是否合法
    @Autowired
    private UserOperLogMapper userOperLogMapper;//产品日志
    @Autowired
    private ProdActiInfoMapper prodActiInfoMapper;//产品日志
     @Autowired
    private SiMuBuyMapper siMuBuyMapper;//私募购买流程
    @Autowired
    private Environment env;
    @Autowired
    private RedisService redisService;
    /**
    * @Description: 私募购买流程页面初始化数据
     * @param custId 客户号
     * @param reserveno 预约编号
    * @Author: zhengyunfei
    * @Date: 2019/1/11
    * @time:17:00
    */
    @GetMapping("/get")
    @ApiOperation(value = "获取数据",notes = "获取数据",position = 1,response = Object.class)
    public Object get(String custId,String reserveno){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        //if (validToken.validToken(token, resultModel)) return resultModel;
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("custId",custId);
        queryMap.put("reserveno",reserveno);
        //获取实名认证信息
        ShiMingRenZhengInfoBo shiMingRenZhengInfoBo=siMuBuyMapper.getShiMingRenZhengInfo(queryMap);
        resultMap.put("smrzInfo",shiMingRenZhengInfoBo);
        //获取投资者认证信息，合同签订状态，填写缴款信息状态
        SiMuByBo siMuByBo= (SiMuByBo) siMuBuyMapper.get(queryMap);
        //投资者认证信息返回结果
        String returnResult= siMuByBo.getRenzhengInfo() ;
        DataKeyValueBo dataKeyValueBo=new DataKeyValueBo();
        DataKeyValueBo renZhengInfoBo=dataKeyValueBo.strToDataKeyValueBo(returnResult);
        resultMap.put("renzhengInfo",renZhengInfoBo);
        //获取合同签订状态
        String heTongQianDingStatus=siMuByBo.getHetongStatus();
        DataKeyValueBo heTongQianDingStatusBo=dataKeyValueBo.strToDataKeyValueBo(heTongQianDingStatus);
        resultMap.put("heTongQianDingStatus",heTongQianDingStatusBo);
        //获取缴款信息状态
        String jiaokuanStatus=siMuByBo.getJiaokuanStatus();
        DataKeyValueBo jiaokuanStatusBo=dataKeyValueBo.strToDataKeyValueBo(jiaokuanStatus);
        resultMap.put("jiaokuanStatus",jiaokuanStatusBo);
        //获取撤销状态
        ProCheXiaoStatusBo proCheXiaoStatusBo=siMuBuyMapper.getCheXiaoStatus(queryMap);
        resultMap.put("proCheXiaoStatus",proCheXiaoStatusBo);
        //获取风险测评问卷状态
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("cCustNo",custId);
        String fxcpStatus=siMuBuyMapper.getFengXianCePingStatus(paramMap);
        resultMap.put("fxcpStatus",fxcpStatus);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(resultMap);
        return resultModel;
    }


    /**
     * @Description: 撤销
     * @param reserveno 预约编号
     * @Author: zhengyunfei
     * @Date: 2019/1/11
     * @time:17:00
     */
    @PostMapping(value = "/chexiao",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "撤销",notes = "撤销",position = 1,response = Object.class)
    public Object chexiao(String reserveno,String projId){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("reserveno",reserveno);
        try{
            siMuBuyMapper.update(queryMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            ProCheXiaoStatusBo proCheXiaoStatusBo=new ProCheXiaoStatusBo();
            proCheXiaoStatusBo.setcStatus("1");
            proCheXiaoStatusBo.setStatusRemark("已撤销");
            resultModel.setData(proCheXiaoStatusBo);
            //撤销成功之后，异步执行
            List<CheXiaoScheduleParam> scheduleList=null;
            if(redisService.exists(CheXiaoScheduleContants.CHEXIAO_SCHEDULE_KEY)){
                scheduleList= (List<CheXiaoScheduleParam>) redisService.get(CheXiaoScheduleContants.CHEXIAO_SCHEDULE_KEY);
            }else{
                scheduleList=new ArrayList<>();
            }
            CheXiaoScheduleParam param=new CheXiaoScheduleParam();
            param.setProjId(projId);
            param.setReserveno(reserveno);
            scheduleList.add(param);
            //重新将计划任务保存到缓存中
            redisService.remove(CheXiaoScheduleContants.CHEXIAO_SCHEDULE_KEY);
            redisService.set(CheXiaoScheduleContants.CHEXIAO_SCHEDULE_KEY,scheduleList);
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }
        return resultModel;
    }


}
